package com.yuxin.tongyi.lsll.thread;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.yuxin.tongyi.lsll.common.Constants;
import com.yuxin.tongyi.lsll.pojo.ClassInfo;
import com.yuxin.tongyi.lsll.util.FileTool;
import com.yuxin.tongyi.lsll.util.PubUtil;

/**
 * @author ww
 * @date:2012-4-7 上午09:03:02
 * @Desc:  获取本地班级数据线程
 * @version :
 * 
 */
public class GetClassDataThread implements Runnable {

	
	private boolean readstate = false;
	private Context context;
	private Handler handler;
	
	public GetClassDataThread(Context context, Handler handler){
		this.context = context;
		this.handler = handler;
	}
	
	@Override
	public void run() {
		Message msg = handler.obtainMessage();
		try {
			while (!readstate) {
				int classState = PubUtil.getConfigValue(context,
						Constants.CFG_CLASS_STATE, 4);
				// 判断class标识， 0：尚未读取完 继续循环，其他:读取SharedPreferences数据
				if (classState == 0) {
					readstate = false;
				} else {
					Bundle bundle = new Bundle();
					String info = PubUtil.getConfigValue(context,
							Constants.CFG_CLASS_DATA, "");
					if (info != null && !"".equals(info)) {
						Object obj = FileTool.getStringToObject(info);
						if (obj instanceof ClassInfo) {
							ClassInfo classObject = (ClassInfo) obj;
							bundle.putSerializable(Constants.STREAM_CLASS_OBJECT, classObject);
							msg.setData(bundle);
						}
					}
					if (classState == Constants.TH_CLASS_EMPTY || classState == Constants.TH_CLASS_ERROR
							|| classState == Constants.TH_CLASS_OFF) {
						classState = Constants.TH_CLASS_OFF;
					}
					msg.what = classState;
					readstate = true;
				}
			}
			handler.sendMessage(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
